"""
Case Type   : 小型化
Case Name   : 执行安装脚本安装单节点
Create At   : 2024/10/25
Owner       : cr13
Description :
    1.获取安装包并解压
    2.执行安装脚本安装单节点
    3.修改权限以及创建数据库
    4.记录数据库端口号
    5.清理数据环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.清理完成
History     :
"""

import os
import subprocess

import unittest
from testcase.utils.Common import Common
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH

from testcase.utils.ComInstall import LiteInstall, LiteUnInstall
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '非1+2环境不执行')
class Lite(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.constant = Constant()
        self.pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.com_install = LiteInstall(self.pkg_path, None, None)
        self.com_uninstall = LiteUnInstall(self.pkg_path)
        self.root_nodes = (Node('PrimaryRoot'),)
        self.user_nodes = (Node('PrimaryDbUser'),)
        self.pri_user_nodes = Node(node='PrimaryDbUser')
        self.pri_root_nodes = Node(node='PrimaryRoot')
        self.user = None
        self.port = None
        self.assert_msg = f'start datanode success'
        self.port_yml = '/home/lite_user/port'
        self.macro_yml = os.path.join(os.path.dirname(macro.SCRIPTS_PATH), '..', 'conf', 'macro.yml')
        self.DB_ENV_PATH = '/home/lite_user/.bashrc'

    def test_upgrade(self):
        text = '----prestep1:获取可用用户和端口----'
        self.log.info(text)
        self.user, self.port = self.com_install.get_user_and_port(
            *self.root_nodes)
        self.user = 'lite_user'
        self.log.info(f'self.user:lite_user, self.port:{self.port}')
        self.user_nodes[0].ssh_user = self.user
        self.user_nodes[0].ssh_password = macro.COMMON_PASSWD

        self.data_path = os.path.join('/home', self.user, 'data')
        self.app_path = os.path.join('/home', self.user, 'app')
        self.com_install.data_path = self.data_path
        self.com_install.app_path = self.app_path

        text = '----prestep2:创建数据库用户和用户组;expect:创建成功----'
        self.log.info(text)
        self.com_install.prepare_user(self.user, *self.root_nodes)

        text = '----step1:获取数据库安装包并解压;expect:成功----'
        self.log.info(text)
        res = self.com_install.tar_pkg(self.root_nodes[0])
        self.assertIn('install.sh', res, f'执行失败: {text}')

        text = '----step2:执行安装脚本安装单节点;expect:执行成功----'
        self.log.info(text)
        param = f'-D {self.data_path} -R {self.app_path} -C "port={self.port}"'
        res = self.com_install.install_single(self.user_nodes[0], self.port,
                                              param)
        self.assertIn(self.assert_msg, res, f'执行失败: {text}')

        text = '----step3:修改权限以及创建数据库;expect:执行成功----'
        self.log.info(text)
        chmod_cmd = f"chmod 755 -R /home/{self.user}"
        self.log.info(chmod_cmd)
        self.pri_root_nodes.sh(chmod_cmd)
        chmod_cmd = f"chmod 700  /home/{self.user}/data"
        self.log.info(chmod_cmd)
        self.pri_root_nodes.sh(chmod_cmd)
        check_cmd = f"su - {self.user} -c \"" \
                    f"gsql -d postgres -p {self.port} " \
                    f"-c \\\"create user {self.pri_user_nodes.db_user} with sysadmin password '{self.pri_user_nodes.db_password}';" \
                    f"create database {self.pri_user_nodes.db_name} owner {self.pri_user_nodes.db_user};\\\"\""
        msg = self.common.get_sh_result(self.pri_root_nodes, check_cmd)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, msg, f'执行失败: {text}')
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, msg, f'执行失败: {text}')
        cmd = "hostname -I | head -1 | awk '{print $NF}'"
        self.log.info(cmd)
        self.cur_ip = subprocess.getoutput(cmd).strip()
        self.log.info(self.cur_ip)

        text = '----step4:记录数据库端口号;expect:记录成功----'
        self.log.info(text)
        write_cmd = f"echo -e '{self.port}' >> {self.port_yml}"
        self.log.info(write_cmd)
        self.pri_root_nodes.sh(write_cmd).result()

    def tearDown(self):
        text = '----step5:清理环境;expect:成功----'
        self.log.info(text)
        text = '----无需清理环境----'
        self.log.info(text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
